package com.caigou.comm.base.container

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.R
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.ext.*
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.comm.base.viewmodel.ReportViewModel
import com.caigou.comm.databinding.ActivityCommentDetailBinding
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NgvChildImageView
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/02/25
 */
@AndroidEntryPoint
open class BaseCommentDetailActivity : BaseActivity() {

    lateinit var binding: ActivityCommentDetailBinding

    val reportViewModel: ReportViewModel by viewModels()

    @Autowired(name = AppConstants.COMMENT_ITEM_BEAN)
    lateinit var commentItemBean: CommentItemBean

     val ngvAdapter: DefaultNgvAdapter<String> by lazy {
        DefaultNgvAdapter(100, GlideDisplay())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCommentDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.commentDetailLlContainer.id
        initView()
    }

    open fun initView() {
        ARouter.getInstance().inject(this)
        binding.commentDetailSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchCommentDetail()
            }
        }

        binding.commentDetailIvAvatar.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Personal.PAGE_PERSONAL_PAGE)
                .withString(AppConstants.USER_ID, commentItemBean.replyUserInfoVo.userId)
                .navigation()
        }

        binding.commentDetailTvBack.setOnClickListener {
            finish()
        }

        LiveDataBus.get().with(BusKey.REPLY_SUCCESS, Any::class.java).observe(this) {
            fetchCommentDetail()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(reportViewModel.replyReportLiveData, ::handleSetReport)
    }

    fun handleGetCommentDetail(result: Resource<CommentItemBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.commentDetailSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取评论详情异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.commentDetailSwipeRefreshLayout.isRefreshing = false

                if (result.data == null) {
                    return
                }
                commentItemBean = result.data
                handleCommentUI()
            }
        }
    }

    fun handleSetCommentLike(result: Resource<String>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.commentDetailSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("点赞失败")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.commentDetailSwipeRefreshLayout.isRefreshing = false
                result.data?.apply {
                    showToast(this)
                    fetchCommentDetail()
                }
            }
        }
    }

    fun handleSetReport(result: Resource<String>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("举报异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                if (result.data == null) {
                    return
                }
                showToast(result.data)
            }
        }
    }

    open fun handleCommentUI() {
        if (TextUtils.isEmpty(commentItemBean.replyUserInfoVo.headImg)) {
            binding.commentDetailIvAvatar.setImageResource(R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(this, commentItemBean.replyUserInfoVo.headImg, binding.commentDetailIvAvatar)
        }
        binding.commentDetailTvNickname.text = commentItemBean.replyUserInfoVo.nickName
        binding.commentDetailTvTime.text = commentItemBean.createTime
        binding.commentDetailTvContent.text = commentItemBean.content

        binding.commentDetailTvLike.text = commentItemBean.nicePoint.toString()
        if (commentItemBean.like) {
            binding.commentDetailIvLike.setImageResource(R.drawable.img_comm_comment_like)
            binding.commentDetailTvLike.setTextColor(Color.parseColor("#FE6880"))
        } else {
            binding.commentDetailIvLike.setImageResource(R.drawable.img_comm_comment_unlike)
            binding.commentDetailTvLike.setTextColor(Color.parseColor("#505050"))
        }
    }

    open fun fetchCommentDetail() {
    }
}